package com.atguigu.jxc.service.impl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.jxc.dao.DamageDao;
import com.atguigu.jxc.dao.UserDao;
import com.atguigu.jxc.entity.DamageList;
import com.atguigu.jxc.entity.DamageListGoods;
import com.atguigu.jxc.entity.DamageListGoodsVo;
import com.atguigu.jxc.entity.User;
import com.atguigu.jxc.service.DamageService;
import com.atguigu.jxc.service.UserService;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.util.JSONPObject;
import com.google.gson.JsonObject;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author dyqstart
 * @create 2021-09-04-15:02
 * @since 1.0.0
 */
@Service
public class DamageServiceImpl implements DamageService {

    @Autowired
    DamageDao damageDao;

    @Autowired
    UserDao userDao;

    @Autowired(required = false)
    private DamageListGoodsVo damageListGoodsVo;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveDamage(DamageList damageList, String damageListGoodsStr) {
//        Integer userId = damageList.getUserId();//TODO 没get到
        User user = userDao.findUserByName((String) SecurityUtils.getSubject().getPrincipal());
        Integer userId = user.getUserId();
        damageList.setUserId(userId);
        damageDao.saveDamageList(damageList);
        Integer damageListId = damageList.getDamageListId();

        try {
//            TypeReference typeReference = new TypeReference<Class<? extends Type>>() {
//                @Override
//                public Type getType() {
//                    return list.getClass().getGenericSuperclass();
//                }
//            };
            List<DamageListGoodsVo> list= new ObjectMapper().readValue(damageListGoodsStr,new TypeReference<List<DamageListGoodsVo>>(){});
            for (DamageListGoodsVo listGoodsVo : list) {
                listGoodsVo.setDamageListId(damageListId);
                damageDao.saveDamageListGoods(listGoodsVo);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }


//        JSONObject[] jsonObjects = {JSONObject.parseObject(damageListGoodsStr)};
//        for (JSONObject jsonObject : jsonObjects) {
//            Object goodsId = jsonObject.get("goodsId");
//            Object goodsCode = jsonObject.get("goodsCode");
//
//            damageListGoodsVo.setGoodsId((Integer) jsonObject.get("goodsId"));
//            damageListGoodsVo.setGoodsCode((String) jsonObject.get("goodsCode"));
//            damageListGoodsVo.setGoodsName((String) jsonObject.get("goodsName"));
//            damageListGoodsVo.setGoodsModel((String) jsonObject.get("goodsModel"));
//            damageListGoodsVo.setGoodsUnit((String) jsonObject.get("goodsUnit"));
//            damageListGoodsVo.setGoodsNum((Integer) jsonObject.get("goodsNum"));
//            damageListGoodsVo.setPrice((Float) jsonObject.get("price"));
//            damageListGoodsVo.setTotal((Float) jsonObject.get("total"));
//            damageListGoodsVo.setLastPurchasingPrice((Float) jsonObject.get("lastPurchasingPrice"));
//            damageListGoodsVo.setGoodsTypeId((Integer) jsonObject.get("goodsTypeId"));
//        }




    }

    @Override
    public Map<String, Object> getDamageList(String sTime, String eTime) {

        List<DamageList> list = damageDao.getDamageList(sTime,eTime);
        HashMap<String, Object> map = new HashMap<>();
        map.put("rows",list);
        return map;
    }

    @Override
    public Map<String, Object> getDamageListGoods(Integer damageListId) {

        List<DamageListGoods> list = damageDao.getDamageListGoods(damageListId);
        HashMap<String, Object> map = new HashMap<>();
        map.put("rows",list);
        return map;
    }
}
